﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using SynergyCounts.View;

namespace SynergyCounts
{
    public sealed partial class SynergyDFCForm : Form, ISynergyDFCView
    {
        #region Fields
        private String[] selectedMCVFilePath;
        private String conditions;
        private String saveXMLFilePath;
        private LogEntry _logEntry;
        #endregion
        #region Properties
        public String[] SelectedMCVFilePath
        {
            get
            {
                return selectedMCVFilePath;
            }
        }
        public String Conditions
        {
            get
            {
                return conditions;
            }
        }
        public String SaveXMLFilePath
        {
            get
            {
                return saveXMLFilePath;
            }
        }
        public Int32 TotalMCVCount
        {
            set
            {
                tbTotalCount.Text = value.ToString();
            }
        }
        public Int32 AccessMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnAccessMCVCount"].Value = value;
            }
        }
        public Int32 BridgeTunnelMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnBridgeTunnelMCVCount"].Value = value;
            }
        }
        public Int32 TollBoothMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTollBoothMCVCount"].Value = value;
            }
        }
        public Int32 UnpavedMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnUnpavedMCVCount"].Value = value;
            }
        }
        public Int32 DOTMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDOTMCVCount"].Value = value;
            }
        }
        public Int32 RDMMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnRDMMCVCount"].Value = value;
            }
        }
        public Int32 DividerMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDividerMCVCount"].Value = value;
            }
        }
        public Int32 GateMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnGateMCVCount"].Value = value;
            }
        }
        public Int32 ZLevelMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnZlevelMCVCount"].Value = value;
            }
        }
        public Int32 TARLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTARLinkCount"].Value = value;
            }
        }
        public Int32 TRDMConditionCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTRDMConditionCount"].Value = value;
            }
        }
        public Int32 TSSSLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTSSSLinkCount"].Value = value;
            }
        }
        public Int32 NoTrucksLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnNoTrucksLinkCount"].Value = value;
            }
        }
        public Int32 TransportTrafficSignLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTransportTrafficSignLinkCount"].Value = value;
            }
        }
        public Int32 TransportMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTransportMCVCount"].Value = value;
            }
        }
        public Int32 POIMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnPOIMCVCount"].Value = value;
            }
        }
        public Int32 TotalLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTotalLinkCount"].Value = value;
            }
        }
        public Int32 DACOMRSEStopMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDACOMRSEStopMCVCount"].Value = value;
            }
        }
        public Int32 ADASMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnADASMCVCount"].Value = value;
            }
        }
        public Int32 TrafficSignalPedTrafficLightMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnTrafficSignalPedTrafficLightMCVCount"].Value = value;
            }
        }
        public Int32 PedCrossingMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnPedCrossingMCVCount"].Value = value;
            }
        }
        public Int32 PriorityMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnPriorityMCVCount"].Value = value;
            }
        }
        //----------start new -------------
        public Int32 DistanceMarkerMCVCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDistanceMarkersMCVCount"].Value = value;
            }
        }

        public Int32 DividerLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDividerLinkCount"].Value = value;
            }
        }
        public Int32 DACOMRSELinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnDACOMRSELinkCount"].Value = value;
            }
        }
        public Int32 SpecialVariableSpeedLinkCount
        {
            set
            {
                dgvDFC.Rows[0].Cells["clmnSpecialVariableSpeedLinkCount"].Value = value;
            }
        }
        // ---------end new ----------------
        public String ExceptionMessage
        {
            set
            {
                tbException.Text = value;
            }
        }
        #endregion
        #region Events
        public event Action LinksSelected;
        public event Action ConditionsSelected;
        public event Action ExportXMLRequested;
        public event Action MCVFileSelected;
        #endregion
        public SynergyDFCForm()
        {
            InitializeComponent();
            btnGetLinks.Click += (sender, e) => InvokeEvent(LinksSelected);
            dgvDFC.LostFocus += (sender, e) => dgvDFC.ClearSelection();
            _logEntry = new LogEntry(Environment.UserName);           
        }

        private void InvokeEvent(Action action)
        {
            if (action != null)
            {
                action.Invoke();
            }
        }

        public void ShowView()
        {
            Application.Run(this);
            //_logEntry.CreateLog();
        }

        public void CloseView()
        {
            Application.Exit();
        }

        private void dgvDFC_DragEnter(Object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.Link;
            }
        }

        private void dgvDFC_DragDrop(Object sender, DragEventArgs e)
        {
            String[] files = (String[])e.Data.GetData(DataFormats.FileDrop);

            foreach (String file in files)
            {
                if (!file.EndsWith(".mcv"))
                {
                    MessageBox.Show("Incorrect *.mcv files");
                    return;
                }
            }
            selectedMCVFilePath = files;
            InvokeEvent(MCVFileSelected);
        }

        private void dgvDFC_KeyDown(Object sender, KeyEventArgs e)
        {
            if ((e.Shift && e.KeyCode == Keys.Insert) || (e.Control && e.KeyCode == Keys.V))
            {
                conditions = Clipboard.GetText(TextDataFormat.UnicodeText);
                InvokeEvent(ConditionsSelected);
            }
        }

        private void btnExportXML_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "XML-file | *.xml";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                saveXMLFilePath = saveFileDialog.FileName;
                InvokeEvent(ExportXMLRequested);
                _logEntry.CreateLog();
            }
        }
        // added button with MCV open file function for tests!!! DELETE AFTER ADDING NEW ISSUES!!!!
        //private void btnMCV_Click(object sender, EventArgs e)
        //{ 
        //    OpenFileDialog openMCV = new OpenFileDialog();
        //    openMCV.InitialDirectory = "D:\\.NET\\Intetics_projects\\dlunev";
        //    openMCV.Filter = "midcover files (*.mcv)|*.mcv|All files (*.*)|*.*";
        //    if (openMCV.ShowDialog() == DialogResult.OK)
        //    {
        //        string[] selectedFile = { openMCV.FileName };
        //        selectedMCVFilePath = selectedFile;
        //    }
        //    InvokeEvent(MCVFileSelected);
        //}
    }
}
